package com.rgz.controller.whdController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.http.HttpRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.rgz.pojo.Carinsuranceid;
import com.rgz.pojo.CarinsuranceidExample;
import com.rgz.pojo.Insurance;
import com.rgz.pojo.InsuranceExample;
import com.rgz.pojo.Insured;
import com.rgz.pojo.Policy;
import com.rgz.pojo.PolicyExample;
import com.rgz.pojo.PolicyExample.Criteria;
import com.rgz.service.WhdService.Whd_autoService;
import com.rgz.utils.BaoDanHao;

@Controller
public class Whd_Testcontroller {
	@Autowired
	private Whd_autoService autoService;

	@RequestMapping("/whd_payment_auto")
	public String auto() {
		
		return "whd_payment_auto";
	}
	
	//自助购保页面的车险购买页面
	@RequestMapping("/whd_inse")
	public String whd_inse() {
		return "whd_insure";
	}
	//自助购保页面
	@RequestMapping("/whd_Insurance")
	public String whd_Insurance() {
		return "whd_Insurance";
	}
	//自助购保页面的三责险购买页面
	@RequestMapping("/whd_insecar")
	public String whd_insecar() {
		
		return "whd_insurecar";
	}
	//自助缴费
	@RequestMapping("/whd_payment")
	public String whd_payment() {
		
		return "whd_payment";
	}
	//成功跳转页面
		@RequestMapping("/whd_chenggong")
		public String whd_chenggong() {
			
			return "whd_chenggong";
		}
	
	
	//一键续保
	@RequestMapping("/whd_renewalOfInsurance")
	public String whd_renewalOfInsurance() {
		
		return "whd_renewalOfInsurance";
	}
	
	// 自助缴费车险页面
	@RequestMapping("/insured")
	public String insured(Model model,String carins,String identity) {
	System.out.println("进来");
		 if (carins.equals("1")){
			 //查询生效中
			CarinsuranceidExample example = new CarinsuranceidExample();
			com.rgz.pojo.CarinsuranceidExample.Criteria criteria = example.createCriteria();
			criteria.andVinEqualTo(identity);
			List<Carinsuranceid> list = autoService.selectByExample(example);
			if (list != null && list.size() > 0){
				//拿出查询的List中的保单号 进行查询
				String policyno = list.get(0).getPolicyno();
				PolicyExample example1 = new PolicyExample();
				Criteria criteria1 = example1.createCriteria();
				criteria1.andPolicynoEqualTo(policyno);
				criteria1.andStatusEqualTo(1);
				List<Policy> list0001 = autoService.selectByExample(example1);
				//查询整个保险类型表
				InsuranceExample example3 = new InsuranceExample();
				com.rgz.pojo.InsuranceExample.Criteria criteria3 = example3.createCriteria();
				List<Insurance> list11 = autoService.selectByExample(example3);
				//给生效中的保险类型赋值
				for (Policy policy : list0001) {
					for (Insurance insurance : list11) {
						if(policy.getInsuranceid()==insurance.getInsuranceid()){
							policy.setInsurancetype(insurance);
						}
					}
				}
				model.addAttribute("list11", list11);
				model.addAttribute("list0001", list0001);
			}
			
			//查询过期中
			CarinsuranceidExample example01 = new CarinsuranceidExample();
			com.rgz.pojo.CarinsuranceidExample.Criteria criteria02 = example01.createCriteria();
			criteria.andVinEqualTo(identity);
			List<Carinsuranceid> list2 = autoService.selectByExample(example01);
			if (list2 != null && list2.size() > 0){
				//拿出查询的List中的保单号 进行查询
				String policyno2 = list.get(0).getPolicyno();
				PolicyExample example2 = new PolicyExample();
				Criteria criteria2 = example2.createCriteria();
				criteria2.andPolicynoEqualTo(policyno2);
				criteria2.andStatusEqualTo(2);
				List<Policy> list0002 = autoService.selectByExample(example2);
				//查询整个保险类型表
				InsuranceExample example3 = new InsuranceExample();
				com.rgz.pojo.InsuranceExample.Criteria criteria3 = example3.createCriteria();
				List<Insurance> list11 = autoService.selectByExample(example3);

				//给已过期的保险类型赋值
				for (Policy policy : list0002) {
					for (Insurance insurance : list11) {
						if(policy.getInsuranceid()==insurance.getInsuranceid()){
							policy.setInsurancetype(insurance);
						}
					}
				}
				model.addAttribute("list11", list11);
				model.addAttribute("list0002", list0002);
			}
			
			
			//查询可续保中
			CarinsuranceidExample example001 = new CarinsuranceidExample();
			com.rgz.pojo.CarinsuranceidExample.Criteria criteria002 = example01.createCriteria();
			criteria.andVinEqualTo(carins);
			List<Carinsuranceid> list02 = autoService.selectByExample(example001);
			if (list2 != null && list2.size() > 0){
				//拿出查询的List中的保单号 进行查询
				String policyno02 = list.get(0).getPolicyno();
				PolicyExample example02 = new PolicyExample();
				Criteria criteria04 = example02.createCriteria();
				criteria04.andPolicynoEqualTo(policyno02);
				criteria04.andStatusEqualTo(3);
				List<Policy> list0003 = autoService.selectByExample(example02);
				
				
				//查询整个保险类型表
				InsuranceExample example3 = new InsuranceExample();
				com.rgz.pojo.InsuranceExample.Criteria criteria3 = example3.createCriteria();
				List<Insurance> list11 = autoService.selectByExample(example3);
			
				//给可续费的保险类型赋值
				for (Policy policy : list0003) {
					for (Insurance insurance : list11) {
						if(policy.getInsuranceid()==insurance.getInsuranceid()){
							policy.setInsurancetype(insurance);
						}
					}
				}	
				model.addAttribute("list11", list11);
				model.addAttribute("list0003", list0003);
			}
		
			return "whd_renewalCar";
			
		}
		//查询生效中
			PolicyExample example = new PolicyExample();
			Criteria criteria = example.createCriteria();
			criteria.andIdentityEqualTo(identity);
			criteria.andStatusEqualTo(1);
			List<Policy> list0001 = autoService.selectByExample(example);
			for (Policy policy : list0001) {
				System.out.println(policy);
			}
			for (Policy policy : list0001) {
				System.out.println(policy.getPolicyno());
			}
			//查询已过期
			PolicyExample example1 = new PolicyExample();
			Criteria criteria1 = example1.createCriteria();
			criteria1.andIdentityEqualTo(identity);
			criteria1.andStatusEqualTo(2);
			List<Policy> list0002 = autoService.selectByExample(example1);
			
			//查询可续费
			PolicyExample example2 = new PolicyExample();
			Criteria criteria2 = example2.createCriteria();
			criteria2.andIdentityEqualTo(identity);
			criteria2.andStatusEqualTo(3);
			List<Policy> list0003 = autoService.selectByExample(example2);
			
			
					
			InsuranceExample example3 = new InsuranceExample();
			com.rgz.pojo.InsuranceExample.Criteria criteria3 = example3.createCriteria();
			List<Insurance> list11 = autoService.selectByExample(example3);
			//给生效中的保险类型赋值
			for (Policy policy : list0001) {
				for (Insurance insurance : list11) {
					if(policy.getInsuranceid()==insurance.getInsuranceid()){
						policy.setInsurancetype(insurance);
					}
				}
			}
			//给已过期的保险类型赋值
			for (Policy policy : list0002) {
				for (Insurance insurance : list11) {
					if(policy.getInsuranceid()==insurance.getInsuranceid()){
						policy.setInsurancetype(insurance);
					}
				}
			}
			//给可续费的保险类型赋值
			for (Policy policy : list0003) {
				for (Insurance insurance : list11) {
					if(policy.getInsuranceid()==insurance.getInsuranceid()){
						policy.setInsurancetype(insurance);
					}
				}
			}	
			model.addAttribute("list11", list11);
			model.addAttribute("list0001", list0001);
			model.addAttribute("list0002", list0002);
			model.addAttribute("list0003", list0003);
				return "whd_renewalCar";
		
	
	}
	@RequestMapping("/qurencar")
	public String qrcar(String policyno,Policy policy){
		//通过保单号查询到保单
		
		PolicyExample example = new PolicyExample();
		Criteria criteria = example.createCriteria();
		
		criteria.andPolicynoEqualTo(policyno);
		
		List<Policy> list = autoService.selectByExample(example);
		
		if (list != null && list.size() > 0){
		Policy policy2 = list.get(0);

		Calendar cl = Calendar.getInstance();
		
		cl.setTime(policy2.getExpire());
		
		cl.add(Calendar.YEAR, 1);
		
		SimpleDateFormat format4 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		String DateToStr4 = format4.format(cl.getTime());
		
		Date parse1 = null;
		try {
			parse1 = format4.parse(DateToStr4);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(parse1);

		PolicyExample example1 = new PolicyExample();
		Criteria criteria1 = example1.createCriteria();
		criteria.andPolicynoEqualTo(policyno);
		policy.setExpire(parse1);
		int i = autoService.updateByExampleSelective(policy, example1);
		System.out.println("添加成功");
		return "redirect:/ccx_home_page";
		}
		System.out.println("添加失败");
		return "redirect:/ccx_home_page";
		
		
	}
	
	
	//我要续保页面的后续页面的购买页面确认页面
	@RequestMapping("/quren")
	public String qr(String policyno,Policy policy){
		//通过保单号查询到保单
		System.out.println(policyno);
		PolicyExample example = new PolicyExample();
		Criteria criteria = example.createCriteria();
		
		criteria.andPolicynoEqualTo(policyno);
		
		List<Policy> list = autoService.selectByExample(example);
		
		if (list != null && list.size() > 0){
		Policy policy2 = list.get(0);

		Calendar cl = Calendar.getInstance();
		
		cl.setTime(policy2.getExpire());
		
		cl.add(Calendar.YEAR, 1);
		
		SimpleDateFormat format4 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		String DateToStr4 = format4.format(cl.getTime());
		
		Date parse1 = null;
		try {
			parse1 = format4.parse(DateToStr4);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(parse1);

		PolicyExample example1 = new PolicyExample();
		Criteria criteria1 = example1.createCriteria();
		criteria.andPolicynoEqualTo(policyno);
		policy.setExpire(parse1);
		int i = autoService.updateByExampleSelective(policy, example1);
		System.out.println("添加成功");
		return "redirect:/ccx_home_page";
		}
		System.out.println("添加失败");
		return "redirect:/ccx_home_page";
		
		
	}
	
	
	//// 我要续保页面的后续页面的购买页面
	@RequestMapping("/whd_xubao")
	public String whd_xubao(Model model,HttpServletRequest request) {
		PolicyExample example = new PolicyExample();
		Criteria criteria = example.createCriteria();
		criteria.andPolicyidEqualTo(Integer.parseInt(request.getParameter("policyid")));
		System.out.println(Integer.parseInt(request.getParameter("policyid")));
		List<Policy> list001 = autoService.selectByExample(example);
				model.addAttribute("list001", list001);
		return "whd_xubao";
	}
	


	//// 我要续保页面的后续页面的车险购买页面
	@RequestMapping("/whd_xubaoCar1")
	public String whd_xubaocar(Model model,HttpServletRequest request) {
		PolicyExample example = new PolicyExample();
		Criteria criteria = example.createCriteria();
		
		criteria.andPolicynoEqualTo(request.getParameter("policyno"));

		
		List<Policy> list001 = autoService.selectByExample(example);
		System.out.println(list001);
				model.addAttribute("list001", list001);
				
		return "whd_xubaoCar";
	}
	// 我要续保页面的后续页面
	@RequestMapping("/renewal")
	public String renewal(Model model, String identity) {
		System.out.println(identity);
		//查询生效中
		PolicyExample example = new PolicyExample();
		Criteria criteria = example.createCriteria();
		criteria.andIdentityEqualTo(identity);
		criteria.andStatusEqualTo(1);
		List<Policy> list01 = autoService.selectByExample(example);
		for (Policy policy : list01) {
			System.out.println(policy.getPolicyno());
		}
		//查询已过期
		PolicyExample example1 = new PolicyExample();
		Criteria criteria1 = example1.createCriteria();
		criteria1.andIdentityEqualTo(identity);
		criteria1.andStatusEqualTo(2);
		List<Policy> list02 = autoService.selectByExample(example1);
		
		//查询可续费
		PolicyExample example2 = new PolicyExample();
		Criteria criteria2 = example2.createCriteria();
		criteria2.andIdentityEqualTo(identity);
		criteria2.andStatusEqualTo(3);
		List<Policy> list03 = autoService.selectByExample(example2);
		
		
				
		InsuranceExample example3 = new InsuranceExample();
		com.rgz.pojo.InsuranceExample.Criteria criteria3 = example3.createCriteria();
		List<Insurance> list = autoService.selectByExample(example3);
		//给生效中的保险类型赋值
		for (Policy policy : list01) {
			for (Insurance insurance : list) {
				if(policy.getInsuranceid()==insurance.getInsuranceid()){
					policy.setInsurancetype(insurance);
				}
			}
		}
		//给已过期的保险类型赋值
		for (Policy policy : list02) {
			for (Insurance insurance : list) {
				if(policy.getInsuranceid()==insurance.getInsuranceid()){
					policy.setInsurancetype(insurance);
				}
			}
		}
		//给可续费的保险类型赋值
		for (Policy policy : list03) {
			for (Insurance insurance : list) {
				if(policy.getInsuranceid()==insurance.getInsuranceid()){
					policy.setInsurancetype(insurance);
				}
			}
		}	
		model.addAttribute("list",list);
		model.addAttribute("list01", list01);
		model.addAttribute("list02", list02);
		model.addAttribute("list03", list03);
			return "whd_renewal";
		}
		
	// 汽车保险添加方法
			@RequestMapping("/inset")
			public String inset(Model model, Policy policy, String identity, int insuranceid, Carinsuranceid carinsuranceid,
					Insured insured) {
				System.out.println(identity);
				BaoDanHao baoDaoHao = new BaoDanHao();
				String uuid = baoDaoHao.getOrderIdByUUId();
				PolicyExample example = new PolicyExample();
				Criteria criteria = example.createCriteria();
				criteria.andIdentityEqualTo(identity);
				criteria.andInsuranceidEqualTo(1);

				List<Policy> list = autoService.selectByExample(example);

				if (list != null && list.size() > 0) {
				Date d1 = new Date();
				Calendar cl = Calendar.getInstance();
				cl.setTime(d1);
				cl.setTime(d1);
				cl.add(Calendar.YEAR, 1);
				SimpleDateFormat format4 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
				String DateToStr4 = format4.format(cl.getTime());
				Date parse = null;
				try {
					parse = format4.parse(DateToStr4);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				Policy policy1 = new Policy();
				policy1.setPolicyno(uuid);
				policy1.setIdentity(policy.getIdentity());
				policy1.setInsuranceid(1);
				policy1.setInsure(new Date());
				policy1.setExpire(parse);
				policy1.setAccidentno(0);
				policy1.setStatus(1);
				boolean flag = autoService.inset(policy1);
					model.addAttribute("",policy1.getPolicyno());
				Carinsuranceid carinsuranceid1 = new Carinsuranceid();
				carinsuranceid1.setVin(carinsuranceid.getVin());
				carinsuranceid1.setCartype(carinsuranceid.getCartype());
				carinsuranceid1.setEngine(carinsuranceid.getEngine());
				carinsuranceid1.setPolicyno(uuid);
				int i = autoService.insert(carinsuranceid1);
					System.out.println("进来了");
				Insured insured2 = new Insured();
				insured2.setInsuredname(insured.getInsuredname());
				insured2.setIdentity(identity);
				insured2.setInsuredphone(insured.getInsuredphone());
				int j = autoService.insert(insured2);
				System.out.println("进来了");
				return "ccx_home_page";	}
				return "ccx_home_page";
			}

	// 查询险种
	@ResponseBody
	@RequestMapping(value = "/Insurance", produces = "application/json;charset=utf-8")
	public String selectByExample1() {
		System.out.println("再见");
		InsuranceExample example = new InsuranceExample();
		com.rgz.pojo.InsuranceExample.Criteria createCriteria = example.createCriteria();
		createCriteria.andInsuranceidEqualTo(1);
		List<Insurance> list = autoService.selectByExample(example);

		for (Insurance insurance : list) {
			System.out.println(insurance);
		}
		String jsonString = JSON.toJSONString(list);
		return jsonString;
	}

	// 三责保险添加方法
	@RequestMapping("/insetcar")
	public String insetcar(Policy policy, String identity, int insuranceid, Carinsuranceid carinsuranceid,
			Insured insured,Model model) {
		System.out.println(identity);
		BaoDanHao baoDaoHao = new BaoDanHao();
		String uuid = baoDaoHao.getOrderIdByUUId();
		PolicyExample example = new PolicyExample();
		Criteria criteria = example.createCriteria();
		criteria.andIdentityEqualTo(identity);
		criteria.andInsuranceidEqualTo(2);

		List<Policy> list = autoService.selectByExample(example);

		Date d3 = new Date();
		Calendar cl = Calendar.getInstance();
		cl.setTime(d3);
		cl.setTime(d3);
		cl.add(Calendar.YEAR, 1);
		SimpleDateFormat format4 = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		String DateToStr4 = format4.format(cl.getTime());
		Date parse = null;
		try {
			parse = format4.parse(DateToStr4);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		Policy policy1 = new Policy();
		policy1.setPolicyno(uuid);
		policy1.setIdentity(policy.getIdentity());
		policy1.setInsuranceid(2);
		policy1.setInsure(new Date());
		policy1.setExpire(parse);
		policy1.setAccidentno(0);
		policy1.setStatus(1);
		boolean flag = autoService.inset(policy1);
		
		model.addAttribute("policyno", policy1.getPolicyno());
		Insured insured2 = new Insured();
		insured2.setInsuredname(insured.getInsuredname());
		insured2.setIdentity(identity);
		insured2.setInsuredphone(insured.getInsuredphone());
		int j = autoService.insert(insured2);
		return "/whd_chenggong";
	}

}
